import java.util.Random;
import java.security.SecureRandom;


public class PasswordManager {
   private static final int OFFSET=3;
   private static final Random RANDOM=new SecureRandom();
   
   public static String Password(String password) {
	   StringBuilder Password=new StringBuilder(password);
       for(int i=0;i<Password.length();i++) {
    	   char c=Password.charAt(i);
    	   c=(char)(c+i+OFFSET);
    	   Password.setCharAt(i, c);
       }
       Password=reverseString(Password);
       return swapFirstAndLastChar(Password.toString());
   }
   public static String decrypt(String Password) {
	   StringBuilder decryptedPassword=new StringBuilder(swapFirstAndLastChar(Password));
	   decryptedPassword=reverseString(decryptedPassword);
	   for(int i=0;i<decryptedPassword.length();i++) {
		   char c=decryptedPassword.charAt(i);
		   c=(char)(c-i-OFFSET);
		   decryptedPassword.setCharAt(i, c);
	   }
	   return decryptedPassword.toString();
   }
   
   private static StringBuilder reverseString(StringBuilder input) {
	   return input.reverse();
   }
   
   private static String swapFirstAndLastChar(String input) {
	   if(input.length()<=1) {
		   return input;
	   }
	   char[] chars=input.toCharArray();
	   char trmp=chars[0];
	   chars[0]=chars[input.length()-1];
	   chars[input.length()-1]=trmp;
	   return new String(chars);
   }
   public static String checkPassword(String password) {
	   int length=password.length();
	   int uppercaseletter=0;
	   int lowercaseletter=0;
	   int digitcount=0;
	   for(int i=0;i<length;i++) {
		   char c=password.charAt(i);
	   if(Character.isUpperCase(c)) {
		   uppercaseletter++;
	   }else if(Character.isLowerCase(c)) {
		   lowercaseletter++;
	   }else if(Character.isDigit(c)) {
		   digitcount++;
	   }
	   }
	   if(length<8||(uppercaseletter==length||lowercaseletter==length||digitcount==length)) {
		   return"弱";
	   }else if(length>=8&&digitcount>0&&(uppercaseletter>0||lowercaseletter>0)) {
		   return"中";
	   }else {
		   return "强";
	   }	       
   }
   
   public static String creatPassword(int length) {
	   StringBuilder password=new StringBuilder(length);
       String uppercaseletter="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       String lowercaseletter="abcdefghijklmnopqrstuvwxyz";
       String digitcount="0123456789";
       String allChars=uppercaseletter+lowercaseletter+digitcount;
        
     password.append(getRandomChar(uppercaseletter));
    password.append(getRandomChar(lowercaseletter));
     password.append(getRandomChar(digitcount));
       
       for(int i=3;i<length;i++) {
    	   password.append(getRandomChar(allChars));
       }
       shuffleString(password);
       return password.toString();
   }
   private static char getRandomChar(String input){
	 return input.charAt(RANDOM.nextInt(input.length()));
   }
   private static void shuffleString(StringBuilder input) {
	   int length=input.length();
	   for(int i=0;i<length-1;i++) {
		   int j=RANDOM.nextInt(length-1)+1;
		   char trmp=input.charAt(j);
		   input.setCharAt(i, input.charAt(j));
		   input.setCharAt(j, trmp);
	   }
   }
   
   public static void main(String[] args) {
	   String password="1234Hello";
	   System.out.println("原始密码："+password);
	   String Password=Password(password);
	   System.out.println("加密后的密码为："+Password);
	   String decryptedPassword=decrypt(Password);
	   System.out.println("解密后的密码："+decryptedPassword);
	   
	   System.out.println(creatPassword(16));
   }
}
